using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;

public partial class ChangePassword : System.Web.UI.Page
{
    protected long Id
    {
        get
        {
            object o = ViewState["Id"];
            if (o != null)
                return (long)o;
            return long.MinValue;
        }
        set
        {
            ViewState["Id"] = value;
        }
    }
    ADESSA.Entities.Member _Member = new ADESSA.Entities.Member();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            return;
        LoadCurent();

    }
    #region LoadCurent
    private void LoadCurent()
    {
        if (Session["MemberId"] != null)
        {
            this.Id = long.Parse(Session["MemberId"].ToString());
            if (this.Id != long.MinValue && this.Id != 0)
            {
                _Member = MemberService.GetMember(this.Id);
                if (_Member != null)
                {
                    ltrEmail.Text = _Member.Email;
                }
            }
            ContentPlaceHolder c = Page.Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
            UserControl usc = c.FindControl("MainMenu1") as UserControl;
            HtmlAnchor ahrChangPass = (HtmlAnchor)usc.FindControl("ahrChangPass");
            ahrChangPass.Attributes.Add("class", "selected1");

        }
        else
            Response.Redirect("MemberLogin.aspx");
    }

    #endregion

    #region Update
    protected void linkNext_Click(object sender, EventArgs e)
    {
        if (!ValiPass(txtPassNew.Value))
        {
            ltrMesM.Visible = true;
        }
        else
        {
            if (this.Id != long.MinValue && this.Id != 0)
            {
                _Member = MemberService.GetMember(this.Id);
                string passOld = txtPassOld.Value.Trim();
                passOld = Utilitys.Encrypt(passOld);
                if (passOld != _Member.Password)
                {
                    ltrMsg.Text = "Password old wrong.";
                    return;
                }
                else
                {
                    _Member.Password = Utilitys.Encrypt(txtPassNew.Value.Trim());
                    MemberService.UpdateMember(_Member);
                    Response.Redirect(string.Format("Announcement.aspx?passmail={0}", _Member.MemberID.ToString()));
                }


            }

        }
    }

    #endregion
    #region Validation Password
    private bool ValiPass(string pass)
    {
        if (pass.Length >= 6 && pass.Length <= 20)
        {
            return true;
        }
        else
            return false;
    }

    #endregion
}
